﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.Model;

namespace dhDataAcess.sysDAL
{
    public class deptListDAL
    {
        string tableName = "deptList";
        private SqlTransaction tran = null;
        private SqlConnection conn = null;
        public deptListDAL(SqlTransaction _transaction, SqlConnection _conn)
        {
            tran = _transaction;
            conn=_conn;
        }

       /*
        /// <summary>
        /// 插入一行部门表数据 
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int deptListInsert(dataAcess.sysDataSet.deptListRow deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
           
            cmd.CommandType = CommandType.Text;
            string insertStr = "insert into deptList (deptNum,deptName,createDate,createUser,updateDate,updateUser,mark) values(@deptNum,@deptName,@createDate,@createUser,@updateDate,@updateUser,@mark)";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",deptRow.deptNum),
                                new SqlParameter ("@deptName",deptRow.deptName),
                                new SqlParameter ("@createDate",deptRow.createDate),
                                new SqlParameter ("@createUser",deptRow.createUser),
                                new SqlParameter ("@updateDate",deptRow.updateDate),
                                new SqlParameter ("@updateUser",deptRow.updateUser),
                                new SqlParameter ("@mark",deptRow.mark)
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }
       */
        /// <summary>
        /// 插入一行部门表数据 
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>

        public int deptListInsert(deptListModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string insertStr = baseBLL.dTableColumnRead.insertSqlStr(tableName ).ToString();
            //string insertStr = "insert into deptList (deptNum,deptName,createDate,createUser,updateDate,updateUser,mark) values(@deptNum,@deptName,@createDate,@createUser,@updateDate,@updateUser,@mark)";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",deptRow.deptnum),
                                new SqlParameter ("@deptName",deptRow.deptname),
                                new SqlParameter ("@createDate",deptRow.createdate),
                                new SqlParameter ("@createUser",deptRow.createuser),
                                new SqlParameter ("@updateDate",deptRow.updatedate),
                                new SqlParameter ("@updateUser",deptRow.updateuser),
                                new SqlParameter ("@mark",deptRow.mark)
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }


        /// <summary>
        /// 更新一行部门表数据
        /// </summary>
        /// <param name="deptRow"></param>
        /// <returns></returns>
 
        public int deptListUpdate(deptListModel deptRow)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string updateStr = baseBLL.dTableColumnRead.updateSqlStr(tableName).ToString();
            //string updateStr = "update deptList set deptName=@deptName,createDate=@createDate,createUser=@createUser,updateDate=@updateDate,updateUser=@updateUser,mark=@mark where deptNum=@deptNum";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",deptRow.deptnum),
                                new SqlParameter ("@deptName",deptRow.deptname),
                                new SqlParameter ("@createDate",deptRow.createdate),
                                new SqlParameter ("@createUser",deptRow.createuser),
                                new SqlParameter ("@updateDate",deptRow.updatedate),
                                new SqlParameter ("@updateUser",deptRow.updateuser),
                                new SqlParameter ("@mark",deptRow.mark)
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = updateStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }


        /// <summary>
        /// 删除一行部门表数据
        /// </summary>
        /// <param name="deptNum"></param>
        /// <returns></returns>
      
        public int deptListDel(string deptNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            string deleteStr = baseBLL.dTableColumnRead.delSqlStr (tableName).ToString();
           // string deleteStr = "delete deptList where deptNum=@deptNum";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",deptNum),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = deleteStr;
            cmd.Transaction = tran;
            int i = cmd.ExecuteNonQuery();
            return i;
        }

        /// <summary>
        /// 返回一个部门名称
        /// </summary>
        /// <param name="deptNum"></param>
        /// <returns></returns>

        public  string deptListRetName(string deptNum)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
          
            string insertStr = "selete deptName from deptList where deptNum=@deptNum";
            SqlParameter[] para = {
                                new SqlParameter ("@deptNum",deptNum),
                              };

            cmd.Parameters.AddRange(para);
            cmd.CommandText = insertStr;
            cmd.Transaction = tran;
            SqlDataReader rdr = cmd.ExecuteReader();
            string deptName = null;
            while (rdr.Read())
            {
                deptName = rdr[0].ToString();
            }

            return deptName;
        }

    }
}
